#include <p18f25k20.h>
#include <stdlib.h>
#include <string.h>
#include "Initializations.h"
#include "Delays.h"
#include "Nokia_LCD_controls.h"


//--------------------------------------------------------------------------
//		Initializations
//--------------------------------------------------------------------------


/* Initializes the oscillator to the proper oscillator selection and system clock */
void oscillator_init(void)
{
	OSCCONbits.IDLEN = 1;		//Device enters idle on SLEEP command
	OSCCONbits.IRCF = 0b111;        //16MHz internal oscillator select
        OSCCONbits.OSTS = 1;            //Device running fron clock defined by FOSC
        OSCCONbits.SCS = 0b00;          //Primary clock determined by FOSC

        OSCTUNEbits.PLLEN = 1;         //PLL disabled

        WDTCONbits.SWDTEN = 0;          //WDT is turned off

	return;
}




/* Set up the desired interrupts and priority levels */
void interrupt_init(void)
{
	INTCON = 0x00;                //disable all interrupts for now
        INTCON2 = 0x00;
        INTCON3 = 0x00;
        
        RCONbits.IPEN = 1;              //enable priority levels
        RCONbits.SBOREN = 0;            //BOR disabled

        IPR1 = 0x00;                    //All peripherals are low priority
        IPR2 = 0x00;                    //
        
        PIR1 = 0x00;                    //Clear all the peripheral interrupt
        PIR2 = 0x00;                    // flags in these registers


        INTCONbits.PEIE = 1;            //Enables all low priority interrupts

        /* Timer 0 interrupt bits */
        INTCONbits.TMR0IE = 1;          //Timer0 interrupt: Enabled
        INTCON2bits.TMR0IP = 0;         //Timer0 priority: Low
        INTCONbits.TMR0IF = 0;          //Clear the timer 0 interrupt flag

        /* Timer 1 interrupt bits */
        PIE1bits.TMR1IE = 1;            //Timer1 interrupt: Enabled
        IPR1bits.TMR1IP = 0;            //Timer1 priority: Low

        /* Timer 2 interrupt bits */
        PIE1bits.TMR2IE = 0;            //Timer2 interrupt: Disabled

        /* Timer 3 interrupt bits */
        PIE2bits.TMR3IE = 0;            //Timer3 interrupt: Diabled

        /* External pin interrupt bits */
        //These external interrupt pins are so that the LED backlight
        //  can come on when a button is pressed
        INTCONbits.INT0IE = 1;          //INT0 external interrupt: Enabled
        INTCON2bits.INTEDG0 = 0;        //INT0 interrupt edge: Falling
        INTCONbits.INT0IF = 0;          //Clear the INT0 interrupt flag
        
        INTCON3bits.INT1IE = 1;         //INT1 external interrupt: Enabled
        INTCON3bits.INT1IP = 0;         //INT1 external interrupt priority: Low
        INTCON2bits.INTEDG1 = 0;        //INT1 interrupt edge: Falling
        INTCON3bits.INT1IF = 0;         //Clear the INT1 interrupt flag

        INTCON3bits.INT2IE = 1;         //INT2 external interrupt: Enabled
        INTCON3bits.INT2IP = 0;         //INT2 external interrupt priority: Low
        INTCON2bits.INTEDG2 = 0;        //INT2 interrupt edge: Falling
        INTCON3bits.INT2IF = 0;         //Clear the INT2 interrupt flag

        /* PORTB interrupt on change bits */
        INTCONbits.RBIE = 1;           //RB port change interrupt: Enabled
        INTCON2bits.RBIP = 0;          //RB port change interrupt priority: Low
        INTCONbits.RBIF = 0;           //Clear RB port change interrupt flag
        INTCON2bits.RBPU = 1;          //All PORTB pull-ups disabled

        IOCB = 0x00;
        IOCBbits.IOCB4 = 1;             //RB4 interrupt on change enabled
        IOCBbits.IOCB5 = 1;             //RB5 interrupt on change enabled

        

        PIE1bits.ADIE = 0;              //A/D converter interrupt: Disabled
        PIE1bits.RCIE = 0;              //EUSART receive interrupt: Disabled
        PIE1bits.TXIE = 0;              //EUSART transmit interrupt: Disabled
        PIE1bits.SSPIE = 0;             //MSSP interrupt: Disabled
        PIE1bits.CCP1IE = 0;            //CCP1 interrupt: Disabled
        PIE2bits.OSCFIE = 0;            //Oscillator fail interrupt: Disabled
        PIE2bits.C1IE = 0;              //C1 interrupt: Disabled
        PIE2bits.C2IE = 0;              //C2 interupt: Disabled
        PIE2bits.EEIE = 0;              //EEPROM/Flash Write operation interrupt: Disabled
        PIE2bits.BCLIE = 0;             //Bus collision interrupt: Disabled



	INTCONbits.GIEH = 1;		  //turn on all high priority interrupts
        

	return;
}


/* Timer 0 generates an interrupt every 5 seconds */
void timer0_init(void)
{


        T0CONbits.T08BIT = 0;           //Configured as an 16 bit timer/counter
        T0CONbits.T0CS = 0;             //Clock from internal instruction cycle clock
        T0CONbits.T0SE = 0;             //Increment on low to high transition
        T0CONbits.PSA = 0;              //Use the prescaler
        T0CONbits.T0PS = 0b111;         //1:256 prescale value

        
        TMR0L = 0xEE;               //16-bit counter, 256 prescaler, pre-set the
        TMR0H = 0x85;               // count to make an overflow every one second

        T0CONbits.TMR0ON = 1;           //Turn it on

	return;
}

/* Timer 1 generates an interrupt every 0.25 seconds */
void timer1_init(void)
{
    T1CONbits.TMR1ON = 0;               //Make sure it starts off
    T1CONbits.RD16 = 0;                 //Read/write of Timer1 in two 8-bit operations
    T1CONbits.T1CKPS = 0;               //1:1 prescale
    T1CONbits.T1OSCEN = 1;              //Timer 1 oscillator is enabled
                                        //  (this is the 32.768kHz external crystal)
    T1CONbits.T1SYNC = 1;               //Do not synchronize external clock
    T1CONbits.TMR1CS = 1;               //Clock source is external clock

    PIE1bits.TMR1IE = 1;		//Turn on the timer 1 interrupt
    IPR1bits.TMR1IP = 1;		//Set it to high priority

    T1CONbits.TMR1ON = 1;               //Turn the thing on now

    return;
}


void timer1_reset(void)
{
	TMR1H = 0x0B;				//Need this count to generate a 0.25 second interrupt
	TMR1L = 0xDB;
	return;
}


void timer2_init(void)
{

    return;
}


/* Sets the I/O port directions for each pin */
void port_init(void)
{
	ADCON0 = 0x00;			//Turn off the A/D converter so it doesnt use my digital I/O pins!
	ADCON1 = 0x00;                  
        ADCON2 = 0x00;

        ANSEL = 0x00;
        ANSELH = 0x00;

        CM1CON0 = 0x00;
        CM2CON0 = 0x00;

        TRISA = 0x00;               //PORTA is LED drivers
        TRISAbits.RA0 = 1;          // and hose switch inputs
        TRISAbits.RA1 = 1;          //

        TRISB = 0xFF;               //PORTB is button inputs

        TRISC = 0x00;               //PORTC is SPI outputs and LCD controls
        TRISCbits.RC4 = 1;          //RC4 is SPI SDIN

	PORTA = 0;
	PORTB = 0;
	PORTC = 0;                  //Make sure everything is set to zero to begin with


	return;
}







